﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 版权所有 (c) 2022 北京超维景生物科技有限公司 保留所有权利。
 * 
 * 创建者：huangyang
 * 电子邮箱：huangyang@tvscope.cn
 * 创建时间：2023/2/22 19:11:22
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>

using ImageK.IO;
using ImageK.Java;
using ImageK.Process;
using Microsoft.VisualBasic.ApplicationServices;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ImageK.Gui
{
    /// <summary>
    /// An ImageRoi is an Roi that overlays an image. 
    /// </summary>
    public class ImageRoi:Roi
    {
        private Image img;
        // private Composite composite;
        private double opacity = 1.0;
        private double angle = 0.0;
        private bool zeroTransparent;
        private ImageProcessor ip;


        /** Creates a new ImageRoi from a BufferedImage.*/
        public ImageRoi(int x, int y, Image bi): base(x, y, bi.Width, bi.Height)
        {
            img = bi;
            setStrokeColor(Color.Black);
        }

        /** Creates a new ImageRoi from a ImageProcessor.*/
        public ImageRoi(int x, int y, ImageProcessor ip): base(x, y, ip.getWidth(), ip.getHeight())
        {
            img = ip.createImage();
            this.ip = ip;
            setStrokeColor(Color.Black);
        }

        public void draw(Graphics g)
        {
            //todo:
            // Graphics2D g2d = (Graphics2D)g;
            // double mag = getMagnification();
            // int sx2 = screenX(x+width);
            // int sy2 = screenY(y+height);
            // Composite saveComposite = null;
            // if (composite!=null)
            // {
            //     saveComposite = g2d.getComposite();
            //     g2d.setComposite(composite);
            // }
            // Image img2 = img;
            // if (angle!=0.0)
            // {
            //     ImageProcessor ip = new ColorProcessor(img);
            //     ip.setInterpolate(true);
            //     ip.setBackgroundValue(0.0);
            //     ip.rotate(angle);
            //     if (zeroTransparent)
            //         ip = makeZeroTransparent(ip, true);
            //     img2 = ip.createImage();
            // }
            // g.drawImage(img2, screenX(x), screenY(y), sx2, sy2, 0, 0, img.getWidth(null), img.getHeight(null), null);
            // if (composite!=null) g2d.setComposite(saveComposite);
            if (isActiveOverlayRoi() && !overlay)
                base.draw(g);
        }

        /** Sets the composite mode. */
        // public void setComposite(Composite composite)
        // {
        //     this.composite = composite;
        // }

        /** Sets the composite mode using the specified opacity (alpha), in the 
             range 0.0-1.0, where 0.0 is fully transparent and 1.0 is fully opaque. */
        public void setOpacity(double opacity)
        {
            if (opacity<0.0) opacity = 0.0;
            if (opacity>1.0) opacity = 1.0;
            this.opacity = opacity;
            // if (opacity!=1.0)
            //     composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float)opacity);
            // else
            //     composite = null;
        }

        /** Returns a serialized version of the image. */
        public byte[] getSerializedImage()
        {
            ImagePlus imp = new ImagePlus("", img);
            return new FileSaver(imp).serialize();
        }

        /** Returns the current opacity. */
        public double getOpacity()
        {
            return opacity;
        }

        public void rotate(double angle)
        {
            this.angle += angle;
        }

        public void setAngle(double angle)
        {
            this.angle = angle;
        }

        public void setZeroTransparent(bool zeroTransparent)
        {
            if (this.zeroTransparent!=zeroTransparent)
            {
                ip = makeZeroTransparent(new ColorProcessor(img), zeroTransparent);
                img = ip.createImage();
            }
            this.zeroTransparent = zeroTransparent;
        }

        public bool getZeroTransparent()
        {
            return zeroTransparent;
        }

        private ImageProcessor makeZeroTransparent(ImageProcessor ip, bool transparent)
        {
            if (transparent)
            {
                //todo:
                // ip.setColorModel(new DirectColorModel(32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000));
                // for (int x = 0; x<width; x++)
                // {
                //     for (int y = 0; y<height; y++)
                //     {
                //         double v = ip.getPixelValue(x, y);
                //         if (v>1)
                //             ip.set(x, y, ip.get(x, y)|0xff000000); // set alpha bits
                //         else
                //             ip.set(x, y, ip.get(x, y)&0xffffff); // clear alpha bits
                //     }
                // }
            }
            return ip;
        }

        public object clone()
        {
            ImageRoi roi2 = (ImageRoi)base.clone();
            ImagePlus imp = new ImagePlus("", img);
            roi2.setProcessor(imp.getProcessor());
            roi2.setOpacity(getOpacity());
            roi2.zeroTransparent = !zeroTransparent;
            roi2.setZeroTransparent(zeroTransparent);
            return roi2;
        }

        public ImageProcessor getProcessor()
        {
            if (ip!=null)
                return ip;
            else
            {
                ip = new ColorProcessor(img);
                return ip;
            }
        }

        public void setProcessor(ImageProcessor ip)
        {
            img = ip.createImage();
            this.ip = ip;
            width = ip.getWidth();
            height = ip.getHeight();
        }
    }
}
